//
//  NewfeatureViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "NewfeatureViewController.h"
#import "TabBarViewController.h"
#import "NavigationController.h"
#define kNewfeatureCount 5

@interface NewfeatureViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIPageControl *pageControl;

@end

@implementation NewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadScrollView];
}

#pragma mark - 创建scrollView
- (void)loadScrollView
{
    // 1、创建一个scrollView:显示所有的新特性
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // 2、添加图片到scrollView中
    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    for (int i = 0; i < kNewfeatureCount; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        // 如果是最后一个imageView 就往里面添加其他内容
        if (i == kNewfeatureCount - 1)
        {
            [self setupLastImageView:imageView];
        }
    }
    
    // 3、设置scrollView的其他属性
    // 如果想要某个方向上不能滚动,那么这个方向对应的尺寸数值传0即可
    scrollView.contentSize = CGSizeMake(kNewfeatureCount * scrollW, 0);
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    
    // 4、添加pageControl 展示目前看到的是第几页
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = kNewfeatureCount;
    pageControl.centerX = scrollW * 0.5;
    pageControl.centerY = scrollH - 50;
    pageControl.userInteractionEnabled = NO;
    pageControl.pageIndicatorTintColor = [UIColor RGBColor:189 green:189 blue:189 alpha:1];
    pageControl.currentPageIndicatorTintColor = [UIColor RGBColor:253 green:98 blue:42 alpha:1];
    self.pageControl = pageControl;
    
    [self.view addSubview:pageControl];
    
}

#pragma mark - 活动的时候改变page
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入计算出页码
    self.pageControl.currentPage = (int)(page + 0.5);
    //    (int)(page + 0.5);
}

#pragma mark - 在最后一个图片上方一个按钮
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 1开启交互功能
    imageView.userInteractionEnabled = YES;
    // 2.开启IT之路
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.frame = CGRectMake(kScreenWidth / 2 - (startBtn.size.width / 2), kScreenHeight / 1.5, startBtn.size.width, startBtn.size.height);
    [startBtn setTitle:@"开启IT之路" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

#pragma mark - 开启IT之路按钮触发事件
- (void)startClick
{
    // 1、创建window对象
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    // 2、创建我的家园对象
    MyHomeViewController *myHoneViewVc = [[MyHomeViewController alloc] init];
    NavigationController *nav = [[NavigationController alloc] initWithRootViewController:myHoneViewVc];
    
    // 3、创建TabBar对象
    TabBarViewController *tabBarViewVc = [[TabBarViewController alloc] init];
    
    // 4、设置抽屉
    MMDrawerController *MMDrawerViewVc = [[MMDrawerController alloc] initWithCenterViewController:tabBarViewVc leftDrawerViewController:nav];
    [MMDrawerViewVc setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [MMDrawerViewVc setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    // 5.设置跟控制器
    window.rootViewController = MMDrawerViewVc;
    
    // 切换到TabBarViewController
    /*
     切换控制器的手段
     1.push：依赖于UINavigationController，控制器的切换是可逆的，比如A切换到B，B又可以回到A
     2.modal：控制器的切换是可逆的，比如A切换到B，B又可以回到A
     3.切换window的rootViewController
     */
    
    // modal方式，不建议采取：新特性控制器不会销毁
    //    TabBarViewController *main = [[TabBarViewController alloc] init];
    //    [self presentViewController:main animated:YES completion:nil];
}

#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}

@end
